layui.use(['form', 'table'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer=layui.layer;
    //form.render();

    table.render({
        elem: '#currentTableId',
        url: bizUrl + '/api/manager/admin/getManagerList',
        method: "get",
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            /*
            userName;
    private String userAccount;
    private String contactName;
    private String userPhone;
    private Integer balance;
    private Integer fee;
    private Date ctime;
    private Integer isFlag;
             */
            {type: "checkbox", width: 50},
            {field: 'userName', minWidth:100, title: '分拣中心名称', sort: true},
            {field: 'userAccount', minWidth:100, title: '账号'},
            {field: 'contactName', minWidth:100, title: '联系人', sort: true},
            {field: 'userPhone', minWidth:100,title: '手机号码'},
            {field: 'balance', title: '账户剩余积分', minWidth: 150},
            {field: 'fee', title:'充值总积分', minWidth: 100},
            {field: 'ctime', minWidth:100, title: '创建时间', sort: true},
           //是否禁用 1 禁用 2 未禁用
            {field: 'isFlag', title:'状态', minWidth: 100,
                templet: function (data) {
                    if(data.isFlag === 2) {
                        return "正常";
                    } else {
                        return "<span style='color: red'>禁用</span>";
                    }
                }},
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]]
        ,response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            ,statusCode: '200' //规定成功的状态码，默认：0
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },
        limits: [5, 10, 15, 20],
        limit: 5,
        page: true,
        //skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        //执行搜索重载
        table.reload('currentTableId', {
            page:{
                curr:1//将页数重新
            },
            where:{
                username:data.field.userName,
                useraccount:data.field.userAccount,
                contactname:data.field.contactName,
                userphone:data.field.userPhone,
                isflag:data.field.isFlag
            }
        });
        return false;
    });

    /*
     * toolbar监听事件
     */
    table.on('tool(currentTableId)', function (obj) {
        let data = obj.data;
        if(obj.event === 'delete') {
            if(confirm("确认要删除吗?")) {
                $.ajax({
                    url: bizUrl + '/api/manager/admin/delManager',
                    type: "delete",
                    data:{managerid:data.managerId},
                    dataType:"json",
                    success: function (result) {
                        layer.alert(result.msg);
                        window.location.reload();
                    }
                });
            }
        }
        if (obj.event === 'edit') {
            let index = layer.open({
                title: '编辑分拣中心',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['50%', '50%'],
                content: 'saveSorting.html',
                success : function(layero, index){
                    let body = layui.layer.getChildFrame('body', index);
                    body.contents().find("[name=managerId]").val(data.managerId);
                    form.render();
                }
            });
            layer.full(index);
            return false;
        }
        if (obj.event === 'buyPoints') {
            layer.open({
                title: '充值积分',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['35%', '30%'],
                content: 'sortingPay.html',
                success : function(layero, index){
                    let body = layui.layer.getChildFrame('body', index);
                    //alert(data.managerId);
                    body.contents().find("[name=managerId]").val(data.managerId);
                    form.render();
                }
            });
          //  layer.full(index);
            return false;
        }
    });
    $("#sortingAdd").click(function () {
        let index = layer.open({
            title: '新增分拣中心',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['50%', '80%'],
            content: 'saveSorting.html'
        });
        layer.full(index);
        return false;
    });


});